package com.bobo.mall.coupon.service.impl;

import com.bobo.common.to.MemberPrice;
import com.bobo.common.to.SkuRedutionTo;
import com.bobo.mall.coupon.entity.MemberPriceEntity;
import com.bobo.mall.coupon.entity.SkuLadderEntity;
import com.bobo.mall.coupon.service.MemberPriceService;
import com.bobo.mall.coupon.service.SkuLadderService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bobo.common.utils.PageUtils;
import com.bobo.common.utils.Query;

import com.bobo.mall.coupon.dao.SkuFullReductionDao;
import com.bobo.mall.coupon.entity.SkuFullReductionEntity;
import com.bobo.mall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    SkuLadderService skuLadderService;
    @Autowired
    MemberPriceService memberPriceService;

    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    public void saveSkuReduction(SkuRedutionTo skuRedutionTo) {
        // 1、5.4）、sku的优惠、满减等信息；gulimall_sms->sms_sku_ladder\sms_sku_full_reduction\sms_member_price
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuRedutionTo.getSkuId());
        skuLadderEntity.setDiscount(skuRedutionTo.getDiscount());
        skuLadderEntity.setFullCount(skuRedutionTo.getFullCount());
        skuLadderEntity.setAddOther(skuRedutionTo.getCountStatus());
        if (skuRedutionTo.getFullCount() > 0){
            skuLadderService.save(skuLadderEntity);
        }


        // 2、sms_sku_full_reduction
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuRedutionTo,skuFullReductionEntity);
        if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal("0"))==1){
            this.save(skuFullReductionEntity);
        }


        // 3、sms_member_price
        List<MemberPrice> memberPrice = skuRedutionTo.getMemberPrice();
        List<MemberPriceEntity> memberPriceEntities = new ArrayList<MemberPriceEntity>();
        for (MemberPrice price : memberPrice) {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            if(price.getPrice().compareTo(new BigDecimal("0"))==1){
                memberPriceEntity.setId(skuRedutionTo.getSkuId());
                memberPriceEntity.setMemberPrice(price.getPrice());
                memberPriceEntity.setMemberLevelId(price.getId());
                memberPriceEntity.setMemberLevelName(price.getName());
                memberPriceEntity.setAddOther(1);
                memberPriceEntities.add(memberPriceEntity);
            }
        }
        memberPriceService.saveBatch(memberPriceEntities);
    }

}